package com.lsh.oms.core.service.es;

import com.lsh.oms.core.constant.EsConstants;
import com.lsh.oms.core.dao.es.EsOperateDao;
import com.lsh.oms.core.enums.EsOperateStatus;
import com.lsh.oms.core.model.es.EsOperate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by panxudong on 16/8/31.
 */
@Component
public class EsOperateService {

    @Autowired
    private EsOperateDao esOperateDao;

    @Transactional(rollbackFor = Exception.class)
    public Integer insert(Integer operateType, Map<String, Object> map) {
        String code = String.valueOf(map.get(EsConstants.ATTR_CODE));
        String orderType = String.valueOf(map.get(EsConstants.ATTR_ORDER_TYPE));

        EsOperate esOperate = new EsOperate();
        esOperate.setOperateType(operateType);
        esOperate.setCode(code != null ? Long.valueOf(code) : null);
        esOperate.setOrderType(orderType != null ? Integer.valueOf(orderType) : null);
        esOperate.setUrl(String.valueOf(map.get(EsConstants.ATTR_URL)));
        esOperate.setData(String.valueOf(map.get(EsConstants.ATTR_DATA)));
        esOperate.setCreateTime((int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
        esOperate.setUpdateTime((int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
        esOperate.setStatus(EsOperateStatus.FAIL.getIndex());
        esOperate.setNumber(0);

        return esOperateDao.insert(esOperate);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer updateStatus(EsOperate esOperate) {
        esOperate.setUpdateTime((int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));

        return esOperateDao.updateStatus(esOperate);
    }

    @Transactional(readOnly = true)
    public List<EsOperate> getEsOperateListByStatus(Integer status, Integer number) {
        return esOperateDao.getEsOperateListByStatus(status, number);
    }

}
